//
// Created by xili on 2024/8/5 21:20.
// Go big or go home.
//
#include <vector>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    TreeNode *constructMaximumBinaryTree(vector<int> &nums) {
        return construct(nums, 0, nums.size());
    }

    TreeNode *construct(vector<int> &nums, int left, int right) {
        if (left > right) {
            return nullptr;
        }
        int max = left;
        for (int i = left + 1; i < right; i++) {
            if (nums[i] > nums[max]) {
                max = i;
            }
        }
        TreeNode *node = new TreeNode(nums[max]);
        node->left = construct(nums, left, max - 1);
        node->right = construct(nums, max + 1, right);
        return node;
    }
};